package com.qilu.pe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.ItemSet;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.ui.activity.ItemSetDetailActivity;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 特色项目/特色套餐
public class SpecialItemFragment extends BaseFragment {
    private RecyclerView rcv_list;
    private SpecialAdapter adapter;

    private String status = "1";// 1 项目 2 套餐
    private List<ItemSet> itemList = new ArrayList<>();

    public SpecialItemFragment() {

    }

    public static SpecialItemFragment newInstance(String status) {
        SpecialItemFragment fragment = new SpecialItemFragment();
        Bundle args = new Bundle();
        args.putString("status", status);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        status = getArguments().getString("status");
        adapter = new SpecialAdapter(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_special_item, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        if (TextUtils.equals("1", status)) {// 套餐
            reqItemList();// 请求项目列表
        } else {// 项目
            reqSetList();// 请求套餐列表
        }

        setListeners();

        return v;
    }

    private void setListeners() {
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("type", status);
                bundle.putString("id", itemList.get(position).getId());
                startActivity(ItemSetDetailActivity.class, bundle);
            }
        });
    }

    // 请求首页项目
    private Disposable reqItemList() {
        return APIRetrofit.getDefault().reqHomeItemList("1")
                .compose(RxUtil.<BaseResult2<List<ItemSet>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<ItemSet>>>() {
                    @Override
                    public void accept(BaseResult2<List<ItemSet>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {

                        } else {

                        }
                        itemList = listBaseResult2.getData();
                        adapter.setDataList(itemList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    // 请求首页套餐
    private Disposable reqSetList() {
        return APIRetrofit.getDefault().reqHomeSetList("1")
                .compose(RxUtil.<BaseResult2<List<ItemSet>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<ItemSet>>>() {
                    @Override
                    public void accept(BaseResult2<List<ItemSet>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {

                        } else {

                        }
                        itemList = listBaseResult2.getData();
                        adapter.setDataList(itemList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    public class SpecialAdapter extends ListBaseAdapter<ItemSet> {

        public SpecialAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_special;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_price = holder.getView(R.id.tv_price);
            TextView tv_content = holder.getView(R.id.tv_content);
            TextView tv_day = holder.getView(R.id.tv_day);

            ItemSet itemSet = mDataList.get(position);
            tv_name.setText(itemSet.getName());
            tv_price.setText("¥" + itemSet.getPrice());
            tv_content.setText(itemSet.getPurpose());
            tv_day.setText(itemSet.getDay() + "个工作日");

            View divider = holder.getView(R.id.divider);
            if (position == itemList.size() - 1) {
                divider.setVisibility(View.INVISIBLE);
            } else {
                divider.setVisibility(View.VISIBLE);
            }
        }
    }
}